// by Xeno
#define THIS_FILE "x_getmtmission.sqf"
#include "x_setup.sqf"

#define __getPos \
_poss = [_target_array2 select 0, _target_array2 select 2] call FUNC(GetRanPointCircleBig);\
while {count _poss == 0} do {\
	_poss = [_target_array2 select 0, _target_array2 select 2] call FUNC(GetRanPointCircleBig);\
	sleep 0.01;\
}

#define __specops \
__GetEGrp(_newgroup)\
_unit_array = [#specops, GVAR(enemy_side)] call FUNC(getunitliste);\
[_poss, _unit_array select 0, _newgroup,true] spawn FUNC(makemgroup);\
sleep 1.0112;\
_newgroup allowFleeing 0;\
_newgroup setVariable [QGVAR(defend), true]; \
[_newgroup, _poss] spawn FUNC(taskDefend); \
[_newgroup, 1] call FUNC(setGState); \
if (GVAR(doRespawnGroups)) then { \
	GVAR(reduce_groups) set [count GVAR(reduce_groups), _newgroup]; \
};


#define __vkilled(ktype) _vehicle addEventHandler [#killed, {_this set [count _this, #ktype]; _this call FUNC(MTSMTargetKilled)}]
//TYRGHEN - Added to handle the new armor of buildings making them harder to destroy
#define __buildingdmg _vehicle addEventHandler [#handleDamage, FUNC(checkmtsidehd)]

private ["_man","_newgroup","_poss","_unit_array","_vehicle","_wp_array","_truck","_the_officer","_sec_kind","_fixor"];
if !(call FUNC(checkSHC)) exitWith {};

_fixor = {
	scriptName "spawn_x_getmtmission_fixor";
	private ["_unit", "_list","_curidx"];
	PARAMS_2(_unit,_curidx);
	sleep 10;
	while {true} do {
		if (!alive _unit || {isNull _unit}) exitWith {};
		sleep 0.01;
		_list = list GVAR(current_trigger);
		#ifndef __TT__
		if ((X_JIPH getVariable QGVAR(campscaptured)) == GVAR(sum_camps) && {("Car" countType _list <= GVAR(car_count_for_target_clear))} && {("Tank" countType _list <= GVAR(tank_count_for_target_clear))} && {("CAManBase" countType _list <= GVAR(man_count_for_target_clear))}) exitWith {};
		#else
		if (((X_JIPH getVariable QGVAR(campscaptured_e)) == GVAR(sum_camps) || {(X_JIPH getVariable QGVAR(campscaptured_w)) == GVAR(sum_camps)}) && {("Car" countType _list <= GVAR(car_count_for_target_clear))} && {("Tank" countType _list <= GVAR(tank_count_for_target_clear))} && {("CAManBase" countType _list <= GVAR(man_count_for_target_clear))}) exitWith {};
		#endif
		
		sleep 3.219;
	};
	if (alive _unit) then {
		sleep 240 + random 60;
		if (alive _unit && _curidx == __XJIPGetVar(GVAR(current_target_index))) then {
			_unit setDamage 1;
			GVAR(side_main_done) = true;
			if (!isServer) then {
				[QGVAR(sSetVar), [QGVAR(side_main_done), true]] call FUNC(NetCallEventCTS);
			};
		} else {
			if (isNull _unit && {!GVAR(side_main_done)} && {_curidx == __XJIPGetVar(GVAR(current_target_index))}) then {
				GVAR(side_main_done) = true;
				if (!isServer) then {
					[QGVAR(sSetVar), [QGVAR(side_main_done), true]] call FUNC(NetCallEventCTS);
				};
			};
		};
	} else {
		if (isNull _unit && {!GVAR(side_main_done)} && {_curidx == __XJIPGetVar(GVAR(current_target_index))}) then {
			GVAR(side_main_done) = true;
			if (!isServer) then {
				[QGVAR(sSetVar), [QGVAR(side_main_done), true]] call FUNC(NetCallEventCTS);
			};
		};
	};
};

_wp_array = _this;

sleep 3.120;
_xx_ran = (count _wp_array) call FUNC(RandomFloor);
_poss = _wp_array select _xx_ran;

_sec_kind = (floor (random 11)) + 1;
// TODO remove me again and make 7,8,9 work
while {_sec_kind in [8]} do {
	_sec_kind = (floor (random 11)) + 1;
};

__TargetInfo

switch (_sec_kind) do {
	case 1: {
		__GetEGrp(_newgroup)
		_the_officer = GVAR(soldier_officer);
		_vehicle = _newgroup createUnit [_the_officer, _poss, [], 0, "FORM"];
		_vehicle call FUNC(removeNVGoggles);
		_vehicle call FUNC(removefak);
		_vehicle call FUNC(removetech);
		_svec = sizeOf _the_officer;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setPos _poss;
		_vehicle setVariable ["BIS_noCoreConversations", true];
		_vehicle setRank "COLONEL";
		_vehicle setSkill 0.3;
		// TYRGHEN - replacing with forcespeed so he can turn
		//_vehicle disableAI "MOVE";
		_vehicle forceSpeed 0;
		if (GVAR(domdatabase)) then {
			_vehicle addEventHandler ["killed", {if (isPlayer (_this select 1)) then {[QGVAR(PAIKP), _this select 1] call FUNC(NetCallEventCTS)}}];
		};
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
#ifndef __TT__
		_iar = __XJIPGetVar(GVAR(searchintel));
		_sum = 0;
		{if (_x == 1) then {__INC(_sum)}} forEach _iar;
		if (_sum < count _iar) then {
			GVAR(intel_unit) = _vehicle;
			[QGVAR(searchbody), _vehicle] call FUNC(NetSetJIP);
			[QGVAR(s_b_client)] call FUNC(NetCallEventToClients);
		} else {
			if (!isNull __XJIPGetVar(GVAR(searchbody))) then {[QGVAR(searchbody), objNull] call FUNC(NetSetJIP)};
		};
		sleep 0.1;
#else
		[_vehicle] call FUNC(TTAddKEH);
#endif
		__vkilled(gov_dead);
		if (GVAR(with_ai) && {GVAR(with_ranked)}) then {
			_vehicle addEventHandler ["killed", {if (!isPlayer (_this select 1)) then {[QGVAR(AddKillAI), [1,_this select 1]] call FUNC(NetCallEventCTS)}}];
		};
		sleep 1.0112;
		__specops;
	};
	case 2: {
		__getPos;
		_ctype = "Land_Radar_Small_F"; // TODO
		_vehicle = createVehicle [_ctype, _poss, [], 0, "NONE"];
		_svec = sizeOf _ctype;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setPos _poss;
		[_vehicle] execFSM "fsms\XRemoveVehiExtra.fsm";
		__vkilled(radar_down);
		__buildingdmg;
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		if (GVAR(domdatabase)) then {
			_vehicle addMPEventHandler ["MPkilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddMTObjKillPoints)}}];
		};
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 3: {
		__getPos;
		_truck = switch (GVAR(enemy_side)) do {
			case "OPFOR": {"rhs_gaz66_repair_msv"};
			case "BLUFOR": {"B_Truck_01_ammo_F"};
			case "INDEPENDENT": {"I_Truck_02_ammo_F"};
		};
		_vehicle = createVehicle [_truck, _poss, [], 0, "NONE"];
		_svec = sizeOf _truck;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		_vehicle lock true;
		_vehicle addEventHandler ["killed", {
			_this set [count _this, "ammo_down"];
			_this call FUNC(MTSMTargetKilled);
			if (GVAR(domdatabase)) then {
				if (isPlayer (_this select 1)) then {[QGVAR(PACKP), _this select 1] call FUNC(NetCallEventCTS)};
			};
			_this call FUNC(handleDeadVec);
		}];
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 4: {
		_truck = switch (GVAR(enemy_side)) do {
			case "OPFOR": {"rhs_gaz66_ap2_msv"};
			case "BLUFOR": {"B_Truck_01_medical_F"};
			case "INDEPENDENT": {"I_Truck_02_medical_F"};
		};
		_vehicle = createVehicle [_truck, _poss, [], 0, "NONE"];
		_svec = sizeOf _truck;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		_vehicle lock true;
		_vehicle addEventHandler ["killed", {
			_this set [count _this, "apc_down"];
			_this call FUNC(MTSMTargetKilled);
			if (GVAR(domdatabase)) then {
				if (isPlayer (_this select 1)) then {[QGVAR(PACKP), _this select 1] call FUNC(NetCallEventCTS)};
			};
			_this call FUNC(handleDeadVec);
		}];
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__specops;
	};
	case 5: {
		__getPos;
		_vehicle = createVehicle [GVAR(enemy_hq), _poss, [], 0, "NONE"];
		_svec = sizeOf GVAR(enemy_hq);
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		_vehicle lock true;
		if (GVAR(domdatabase)) then {
			_vehicle addMPEventHandler ["MPkilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddMTObjKillPoints)}}];
		};
		[_vehicle] execFSM "fsms\XRemoveVehiExtra.fsm";
		__vkilled(hq_down);
		__buildingdmg;
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 6: {
		__getPos;
		_fact = switch (GVAR(enemy_side)) do {
			case "OPFOR": {"Land_dp_transformer_F"}; // TODO
			case "BLUFOR": {"Land_dp_transformer_F"};
			case "INDEPENDENT": {"TK_GUE_WarfareBLightFactory_EP1"};
		};
		_vehicle = createVehicle [_fact, _poss, [], 0, "NONE"];
		_svec = sizeOf _fact;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		[_vehicle] execFSM "fsms\XRemoveVehiExtra.fsm";
		__vkilled(light_down);
		__buildingdmg;
		if (GVAR(domdatabase)) then {
			_vehicle addMPEventHandler ["MPkilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddMTObjKillPoints)}}];
		};
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 7: {
		__getPos;
		/*_fact = switch (GVAR(enemy_side)) do {
			case "OPFOR": {"Land_spp_Transformer_F"};
			case "BLUFOR": {"Land_spp_Transformer_F"};
			case "INDEPENDENT": {"Land_spp_Transformer_F"};
		};*/
		_fact = "Land_spp_Transformer_F";
		_vehicle = createVehicle [_fact, _poss, [], 0, "NONE"];
		_svec = sizeOf _fact;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		[_vehicle] execFSM "fsms\XRemoveVehiExtra.fsm";
		__vkilled(heavy_down);
		__buildingdmg;
		if (GVAR(domdatabase)) then {
			_vehicle addMPEventHandler ["MPkilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddMTObjKillPoints)}}];
		};
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__GetEGrp(_newgroup)
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 8: {
		__getPos;
		_vehicle = createVehicle [GVAR(artillery_radar), _poss, [], 0, "NONE"];
		_svec = sizeOf GVAR(artillery_radar);
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		[_vehicle] execFSM "fsms\XRemoveVehiExtra.fsm";
		__vkilled(artrad_down);
		__buildingdmg;
		if (GVAR(domdatabase)) then {
			_vehicle addMPEventHandler ["MPkilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddMTObjKillPoints)}}];
		};
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 9: {
		__getPos;
		_vehicle = createVehicle [GVAR(air_radar), _poss, [], 0, "NONE"];
		_svec = sizeOf GVAR(air_radar);
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setDir (floor random 360);
		_vehicle setPos _poss;
		[_vehicle] execFSM "fsms\XRemoveVehiExtra.fsm";
		__vkilled(airrad_down);
		__buildingdmg;
		if (GVAR(domdatabase)) then {
			_vehicle addMPEventHandler ["MPkilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddMTObjKillPoints)}}];
		};
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
	case 10: {
		__GetEGrp(_newgroup)
		_ctype = "C_man_polo_6_F"; // TODO: Hopefully A3 has more than the current shitty simple beach party civilians, it was a Dr. before. NO IT DOESN'T
		_vehicle = _newgroup createUnit [_ctype, _poss, [], 0, "FORM"];
		_vehicle call FUNC(removeNVGoggles);
		_vehicle call FUNC(removefak);
		_svec = sizeOf _ctype;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setPos _poss;
		_vehicle setVariable ["BIS_noCoreConversations", true];
		_vehicle setRank "COLONEL";
		_vehicle setSkill 0.3;
		// TYRGHEN - replacing with forcespeed so he can turn
		_vehicle disableAI "MOVE";
		_vehicle forceSpeed 0;
		if (GVAR(domdatabase)) then {
			_vehicle addEventHandler ["killed", {if (isPlayer (_this select 1)) then {[QGVAR(PAIKP), _this select 1] call FUNC(NetCallEventCTS)}}];
		};
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		_vehicle addMagazines ["16Rnd_9x21_Mag", 2];
		_vehicle addWeapon "hgun_Rook40_F";
#ifndef __TT__
		_iar = __XJIPGetVar(GVAR(searchintel));
		_sum = 0;
		{if (_x == 1) then {__INC(_sum)}} forEach _iar;
		if (_sum < count _iar) then {
			GVAR(intel_unit) = _vehicle;
			[QGVAR(searchbody), _vehicle] call FUNC(NetSetJIP);
			[QGVAR(s_b_client)] call FUNC(NetCallEventToClients);
		} else {
			if (!isNull __XJIPGetVar(GVAR(searchbody))) then {[QGVAR(searchbody), objNull] call FUNC(NetSetJIP)};
		};
		sleep 0.1;
#else
		[_vehicle] call FUNC(TTAddKEH);
#endif
		__vkilled(lopo_dead);
		if (GVAR(with_ai) && {GVAR(with_ranked)}) then {
			_vehicle addEventHandler ["killed", {if (!isPlayer (_this select 1)) then {[QGVAR(AddKillAI), [1,_this select 1]] call FUNC(NetCallEventCTS)}}];
		};
		sleep 1.0112;
		__specops;
	};
	case 11: {
		__GetEGrp(_newgroup)
		_ctype = "C_man_1_3_F"; // TODO: It was a civ EuroMan before
		_vehicle = _newgroup createUnit [_ctype, _poss, [], 0, "FORM"];
		_vehicle call FUNC(removeNVGoggles);
		_vehicle call FUNC(removefak);
		_svec = sizeOf _ctype;
		_isFlat = (position _vehicle) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _vehicle];
		if (count _isFlat > 1) then {
			_isFlat set [2,0];
			if (_poss distance _isFlat < 100) then {
				_poss = _isFlat;
			};
		};
		_vehicle setPos _poss;
		_vehicle setVariable ["BIS_noCoreConversations", true];
		_vehicle setRank "COLONEL";
		_vehicle setSkill 0.3;
		[_vehicle, __XJIPGetVar(GVAR(current_target_index))] spawn _fixor;
		// TYRGHEN - replacing with forcespeed so he can turn
		_vehicle disableAI "MOVE";
		_vehicle forceSpeed 0;
		if (GVAR(domdatabase)) then {
			_vehicle addEventHandler ["killed", {if (isPlayer (_this select 1)) then {[QGVAR(PAIKP), _this select 1] call FUNC(NetCallEventCTS)}}];
		};
		_vehicle addMagazines ["16Rnd_9x21_Mag", 2];
		_vehicle addWeapon "hgun_Rook40_F";
#ifndef __TT__
		_iar = __XJIPGetVar(GVAR(searchintel));
		_sum = 0;
		{if (_x == 1) then {__INC(_sum)}} forEach _iar;
		if (_sum < count _iar) then {
			GVAR(intel_unit) = _vehicle;
			[QGVAR(searchbody), _vehicle] call FUNC(NetSetJIP);
			[QGVAR(s_b_client)] call FUNC(NetCallEventToClients);
		} else {
			if (!isNull __XJIPGetVar(GVAR(searchbody))) then {[QGVAR(searchbody), objNull] call FUNC(NetSetJIP)};
		};
		sleep 0.1;
#endif
		__vkilled(dealer_dead);
		if (GVAR(with_ai) && {GVAR(with_ranked)}) then {
			_vehicle addEventHandler ["killed", {if (!isPlayer (_this select 1)) then {[QGVAR(AddKillAI), [1,_this select 1]] call FUNC(NetCallEventCTS)}}];
		};
		sleep 1.0112;
		__specops;
#ifdef __TT__
		[_vehicle] call FUNC(TTAddKEH);
#endif
	};
};

["sec_kind",_sec_kind] call FUNC(NetSetJIP);
_s = "";
if (__XJIPGetVar(GVAR(current_target_index)) != -1) then {
	_s = (switch (_sec_kind) do {
		case 1: {
			format [(localize "STR_DOM_MISSIONSTRING_891"), _current_target_name]
		};
		case 2: {
			format [(localize "STR_DOM_MISSIONSTRING_893"), _current_target_name]
		};
		case 3: {
			#ifndef __TT__
			format [(localize "STR_DOM_MISSIONSTRING_894"), _current_target_name]
			#else
			format [(localize "STR_DOM_MISSIONSTRING_895"), _current_target_name]
			#endif
		};
		case 4: {
			#ifndef __TT__
			format [(localize "STR_DOM_MISSIONSTRING_896"), _current_target_name]
			#else
			format [(localize "STR_DOM_MISSIONSTRING_897"), _current_target_name]
			#endif
		};
		case 5: {
			format [(localize "STR_DOM_MISSIONSTRING_898"), _current_target_name]
		};
		case 6: {
			format [(localize "STR_DOM_MISSIONSTRING_899"), _current_target_name]
		};
		case 7: {
			format [(localize "STR_DOM_MISSIONSTRING_900"), _current_target_name]
		};
		case 8: {
			format [(localize "STR_DOM_MISSIONSTRING_901"), _current_target_name]
		};
		case 9: {
			format [(localize "STR_DOM_MISSIONSTRING_902"), _current_target_name]
		};
		case 10: {
			format [(localize "STR_DOM_MISSIONSTRING_903"), _current_target_name]
		};
		case 11: {
			format [(localize "STR_DOM_MISSIONSTRING_904"), _current_target_name]
		};
	});
} else {
	_s = (localize "STR_DOM_MISSIONSTRING_905");
};
#ifndef __TT__
[QGVAR(kbmsg), [18, _s]] call FUNC(NetCallEventCTS);
#else
[QGVAR(kbmsg), [19, _s]] call FUNC(NetCallEventCTS);
#endif
